﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;



/// <summary>
/// 只要继承自PageBase就可以用到所有功能。
/// </summary>
public partial class MemberPortal_TestPrivilege : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            lbl_SessionId.Text = Session.SessionID;
            StringBuilder output = new StringBuilder();
            for (int i = 0; i < Request.Cookies.Count; i++)
            {
                HttpCookie aCookie = Request.Cookies[i];
                output.Append("Name = " + aCookie.Name + "<br />");
                if (aCookie.HasKeys)
                {
                    for (int j = 0; j < aCookie.Values.Count; j++)
                    {
                        string subkeyName = Server.HtmlEncode(aCookie.Values.AllKeys[j]);
                        string subkeyValue = Server.HtmlEncode(aCookie.Values[j]);
                        output.Append("Subkey name = " + subkeyName + "<br />");
                        output.Append("Subkey value = " + subkeyValue +
                            "<br /><br />");
                    }
                }
                else
                {
                    output.Append("Value = " + Server.HtmlEncode(aCookie.Value) +
                        "<br /><br />");
                }
            }
            lbl_Cookie.Text = output.ToString();

            #region 获得用户权限

            StringBuilder sp = new StringBuilder();

            sp.Append("<br/> Privilege.所有模块.基础资料.ProviderInfo.View ,是否拥有权限：" + PageBase.GetPrivilege(Privilege.所有模块.基础资料.ProviderInfo.View).ToString());

            lbl_Privilege.Text = sp.ToString();
            #endregion

            #region 系统可以自动获取当前用户的基本信息
            MembershipUser u = Membership.GetUser();

            StringBuilder BaseInfo = new StringBuilder("用户Guid ProviderUserKey：" + u.ProviderUserKey);


            BaseInfo.Append("<br/>提供者 ProviderName：" + u.ProviderName);
            BaseInfo.Append("<br/>电子邮件 Email：" + u.Email);
            BaseInfo.Append("<br/>密码的问题 PasswordQuestion：" + u.PasswordQuestion);
            BaseInfo.Append("<br/>备注 Comment：" + u.Comment);
            BaseInfo.Append("<br/>是否审核 IsApproved：" + u.IsApproved);
            BaseInfo.Append("<br/>是否锁定 IsLockedOut：" + u.IsLockedOut);
            BaseInfo.Append("<br/>是否在线 IsOnline：" + u.IsOnline);
            BaseInfo.Append("<br/>创建时间 CreationDate：" + u.CreationDate);
            BaseInfo.Append("<br/>最后在线时间 LastActivityDate：" + u.LastActivityDate);
            BaseInfo.Append("<br/>最后锁定时间 LastLockoutDate：" + u.LastLockoutDate);
            BaseInfo.Append("<br/>最后登陆时间 LastLoginDate：" + u.LastLoginDate);
            BaseInfo.Append("<br/>最后密码改变时间 LastPasswordChangedDate：" + u.LastPasswordChangedDate);

            lbl_BaseInfo.Text = BaseInfo.ToString();

            #endregion

            #region 获得用户扩展信息
            if (UserDetail() != null)
            {
                StringBuilder ExtendInfo = new StringBuilder();


                ExtendInfo.Append("<br/> [UserId]：" + UserDetail().UserId);
                ExtendInfo.Append("<br/> [UserNo]：" + UserDetail().UserNo);
                ExtendInfo.Append("<br/> [TrueName]：" + UserDetail().TrueName);
                ExtendInfo.Append("<br/> [NickName]：" + UserDetail().NickName);
                ExtendInfo.Append("<br/> [Account]：" + UserDetail().Account);
                ExtendInfo.Append("<br/> [IdCard]：" + UserDetail().IdCard);
                ExtendInfo.Append("<br/>");



                ExtendInfo.Append("<br/> [BirthDay]：" + UserDetail().BirthDay);
                ExtendInfo.Append("<br/> [Blog]：" + UserDetail().Blog);
                ExtendInfo.Append("<br/> [Interest]：" + UserDetail().Interest);
                ExtendInfo.Append("<br/> [Profession]：" + UserDetail().Profession);
                ExtendInfo.Append("<br/> [QQ]：" + UserDetail().QQ);
                ExtendInfo.Append("<br/>");

                ExtendInfo.Append("<br/> [Country]：" + UserDetail().Country);
                ExtendInfo.Append("<br/> [Province]：" + UserDetail().Province);
                ExtendInfo.Append("<br/> [City]：" + UserDetail().City);
                ExtendInfo.Append("<br/> [Address]：" + UserDetail().Address);
                ExtendInfo.Append("<br/> [Postcode]：" + UserDetail().Postcode);
                ExtendInfo.Append("<br/>");

                ExtendInfo.Append("<br/> [Image]：" + UserDetail().Image);
                ExtendInfo.Append("<br/> [ImageType]：" + UserDetail().ImageType);
                ExtendInfo.Append("<br/> [EnableImage]：" + UserDetail().EnableImage);
                ExtendInfo.Append("<br/> [PrivateEmail]：" + UserDetail().PrivateEmail);
                ExtendInfo.Append("<br/> [EnableReviceEmail]：" + UserDetail().EnableReviceEmail);
                ExtendInfo.Append("<br/> [Signature]：" + UserDetail().Signature);
                ExtendInfo.Append("<br/> [EnableSign]：" + UserDetail().EnableSign);

                lbl_ExtendInfo.Text = ExtendInfo.ToString();
            }
            else
            {
                lbl_ExtendInfo.Text = "此用户无扩展信息";
            }
            #endregion

            #region 获得用户所属角色
            string[] roleNames = Roles.GetRolesForUser(u.UserName);

            foreach (string s in roleNames)
            {
                lbl_Roles.Text = s + ", " + lbl_Roles.Text;
            }
            #endregion

            #region rolePrincipal测试

            StringBuilder rolePrincipal = new StringBuilder();

            RolePrincipal r = new RolePrincipal(User.Identity);

            rolePrincipal.Append("<br/> [CookiePath]:" + r.CookiePath);
            rolePrincipal.Append("<br/> [CachedListChanged]:" + r.CachedListChanged);
            rolePrincipal.Append("<br/> [Expired]:" + r.Expired);
            rolePrincipal.Append("<br/> [ExpireDate]:" + r.ExpireDate);
            rolePrincipal.Append("<br/> [Identity,AuthenticationType]:" + r.Identity.AuthenticationType);
            rolePrincipal.Append("<br/> [Identity,IsAuthenticated]:" + r.Identity.IsAuthenticated);
            rolePrincipal.Append("<br/> [Identity,Name]:" + r.Identity.Name);
            rolePrincipal.Append("<br/> [Version]:" + r.Version);
            roleNames = r.GetRoles();
            StringBuilder roles = new StringBuilder();
            foreach (string s in roleNames)
            {
                roles.Append(s + ", ");
            }

            rolePrincipal.Append("<br/> [GetRoles]:" + roles.ToString());
            rolePrincipal.Append("<br/> [IssueDate]:" + r.IssueDate.ToString());


            lbl_RolePrincipal.Text = rolePrincipal.ToString();

            #endregion

        }
    }

}
